package com.he.algorithm.SequenceList;

import java.util.Arrays;

/**
 * @description: TODO
 * @Author: HCL
 * @Date: 2024/8/27 15:48
 */
public class SequenceUtil {

    /**
     * @Description 往数组的指定位置插入数据, 并返回新数组
     * @Author HCL
     * @Date 2024/8/27 15:50
     * @Param [nums, target, value]
     * @Return void
     */
    public static int[] insert(int[] nums, int target, int value) {
        int len = nums.length;
        int[] data = new int[len + 1];
        System.arraycopy(nums, 0, data, 0, nums.length);//复制数组到新数组
        for (int i = len; i > target; i--) {
            data[i] = data[i - 1];//从末尾开始向后移动
        }
        data[target] = value;//赋值
        return data;
    }

    public static int[] insert2(int[] nums, int target, int value) {
        int[] data = new int[nums.length + 1];
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i == target){
                data[i] = value;
             } else if (i > target) {
                data[i] = nums[count++];
            }else {
                data[i] = nums[count++];
            }
        }
        return data;
    }


    //list更方便
    /**
    * @Description 删除某个位置元素
    * @Author HCL
    * @Date 2024/8/27 17:10
    * @Param [nums, target]
    * @Return int[]
    */
    public static int[] delete(int[] nums, int target) {
        int[] data = new int[nums.length - 1];
        int count = 0;
        for (int i = 0; i < nums.length; i++) {
            if (i != target){
                data[count++] = nums[i];
            }
        }
        return data;
    }

    public static void main(String[] args) {
        int[] nums = {1, 2, 3, 4, 5};
        //将6插入到第2个位置
//        int[] insert = insert(nums, 2, 6);
//        System.out.println(Arrays.toString(insert));

        //删除第1个位置的数
        int[] delete = delete(nums, 1);
        System.out.println(Arrays.toString(delete));
    }
}
